package com.ymatou.iapi.livebuyer.testcase;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.livebuyer.parameter.AddLiveActivityInJobBean;
import com.ymatou.iapi.livebuyer.service.AddLiveActivityInJobCall;
import com.ymatou.iapi.liveseller.parameter.CreateLiveActivityBean;
import com.ymatou.iapi.liveseller.parameter.CreateSellerLiveBean;
import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.LiveRoomCallService;
import com.ymttest.business.service.ProductManagerCallServiceV2;
import com.ymttest.database.model.LiveRoom;
import com.ymttest.database.model.YmtCatalogsI;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.GetLog;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;
/**
*秒杀活动加入JOB
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_AddLiveActivityInJob{
	private static AddLiveActivityInJobBean addliveactivityinjobBean;
	private static AddLiveActivityInJobCall addliveactivityinjobCall;
	private static int sellerId = Integer.parseInt(EnvSetup.getData("autotest_seller1"));
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("秒杀活动加入JOB");
	}

	@Before
	public void caseUp() {
		addliveactivityinjobBean=new AddLiveActivityInJobBean();
		addliveactivityinjobCall=new AddLiveActivityInJobCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(addliveactivityinjobCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_AddLiveActivityInJob_001() {
		Logger.start(true,"秒杀活动加入JOB成功");
		try {
			Logger.comment("创建商品");
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建直播");
			// 5分钟后开始200分钟后结束
			CreateSellerLiveBean clive = LiveRoomCallService.initCreatesellerliveBean(sellerId);
			Date now = YMTDateUtil.getDate();
			//半小时之内会锁定不能修改
			Date starttime = new Date(now.getTime() +  10 * 1000);
			Date endtime = new Date(now.getTime() + 200 * 60 * 1000);
			clive.getLiveDto().setStartTime(YMTDateUtil.getFormatTime(starttime, YMTDateUtil.YMDHMS));
			
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(clive);
			Logger.comment("加入商品");
			LiveRoomCallService.AddProductsToLive(createlive.getId(), sellerId, productId);
			Logger.comment("查询商品规格");
			YmtCatalogsIWapper CIWapper = new YmtCatalogsIWapper();
			List<String> cids = CIWapper.selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
			Logger.comment("创建秒杀活动");
			Date starttime2 = new Date(now.getTime() +  10 * 1000);
			CreateLiveActivityBean createliveactivityBean = LiveRoomCallService.initCreateLiveActivityBean(createlive.getId(), cids.get(0), 5d, 6, starttime2);
			LiveRoomCallService.CreateLiveActivity(createliveactivityBean);
			GetLog getLog=new GetLog("172.16.101.117", "livebuyer.iapi.ymatou.com");
			String reqid=TestDataManager.getRandomUUID().toString().replaceAll("-", "");
			getLog.startListenVm("default.log",reqid);
			addliveactivityinjobBean.setAppId("autotest");
			addliveactivityinjobBean.setRequestId(reqid);
			addliveactivityinjobCall.setData(addliveactivityinjobBean);
			addliveactivityinjobCall.callService();			
			Logger.verifyEquals("ok",addliveactivityinjobCall.getReturnData(),"验证返回");
			Thread.sleep(3000);
			getLog.endListen();
			ArrayList<String> loglist = getLog.getLastLog();
			//验证日志
			loglist.forEach(log->Logger.debug(log));
			Logger.verifyEquals(true,loglist.size()>2,"验证日志2条以上");
			//liveactivity.BuyerNotify "bizType":202 bookTime 提前 5s
//			liveactivity.SellerNotify "bizType":201 bookTime 提前 1 min
			//createTask req:TaskItem[taskName=LiveVideo.LiveActivityStartNotify,bizID=100000001,bookTime=2018-07-23 12:08:47,callbackParam=SendLiveActivityStartMessageReq:{"bizType":202,"liveActivityId":100000001,"liveId":11088}]
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}